06. Demo: Fixing Errors in Numeric Data Using Pandas

PRTDM2-785 AI Trading C2 L1 Vid8 Demo

Erratum: In the video at 4:04, the instructor refers to the escape character as a forward slash (/). This is incorrect. The correct symbol used for escape sequences in programming is the backslash ().

Addressing Common Pandas Data Issues

Key Concepts:

  • Data Importation Challenges: Sometimes when importing datasets into Pandas, numeric data might be read as strings because of characters like dollar signs or commas.

Problem Areas:

  • Non-Numeric Characters:
    • Price Column: Contains unwanted dollar signs.
    • Revenue Column: Uses commas for the thousand separators.
    • Quantity Column: May have strings where numbers are expected.

Solutions:

  1. Recognizing Data Types:

    • Use .info() to identify data types and check if columns are mistakenly read as objects (strings).
  2. Cleaning Up Data:

    • Replacing Characters:
      • Remove dollar signs using the .replace() method with regex enabled.
      • Remove commas similarly.
    • Converting to Numeric:
      • Use .astype(float) to convert cleaned data to numeric values.
  3. Dealing with Errors:

    • Use pd.to_numeric() with errors='coerce' for converting strings to NaN where they can't be made numeric initially.

Outcome:

  • Post-cleaning, data is converted to appropriate numeric types, enabling accurate analysis and manipulation.